package com.firmye.codereposboot.utils;

import com.firmye.codereposboot.model.commons.Month;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author firmye
 * @Date 2017年12月27日 下午1:55:55
 *
 * @Description 
 */
public class DateUtil {
	
	/**
	 * @Author firmye
	 * @Date 2017年12月27日 下午1:59:40
	 *
	 * @Description pattern为null，默认格式"yyyy-MM-dd HH:mm:ss"
	 */
	public static String parseDate(Date date, String pattern) {
		if(null == date) {
			return null;
		}
		if(null == pattern) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}
	
	/**
	 * @Author Firmye
	 * @Date 2017年12月29日 上午1:12:46
	 *
	 * @Description pattern为null，默认格式"yyyy-MM-dd HH:mm:ss"
	 */
	public static Date parseStr(String str, String pattern) throws ParseException {
		if(null == str) {
			return null;
		}
		if(null == pattern) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.parse(str);
	}

	public static List<Month> getMonths(Integer year) {
		List<Month> months = new ArrayList<>();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		for(int i = 0;i < 12;i++){
			// Month month = new Month();
			cal.set(Calendar.MONTH,i);
			Month month = getMonth(cal.getTime());
			// month.setMonth(i + 1);
			// cal.set(Calendar.DATE, 1);	// 把日期设置为当月第一天
			// cal.roll(Calendar.DATE, -1);	// 日期回滚一天，也就是最后一天
			// for(int j = 0;j < cal.get(Calendar.DATE);j++){
			// 	month.addDates(j + 1);
			// }
			months.add(month);
		}
		return months;
	}

	public static Date getMonthStart(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获取某月最小天数
		int firstDay = cal.getMinimum(Calendar.DATE);
		// 设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH,firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND,0);
		return cal.getTime();
	}

	public static Date getMonth(Integer year,Integer month){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		return cal.getTime();
	}

	public static Month getMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		Month month = new Month();
		month.setMonth(cal.get(Calendar.MONTH) + 1);
		cal.set(Calendar.DATE, 1);	// 把日期设置为当月第一天
		cal.roll(Calendar.DATE, -1);	// 日期回滚一天，也就是最后一天
		for(int j = 0;j < cal.get(Calendar.DATE);j++){
			month.addDates(j + 1);
		}

		return month;
	}

	public static Date getDay(Date date, Integer day){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, day);
		return cal.getTime();
	}

	public static Date getDayStart(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND,0);
		return cal.getTime();
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(parseStr("2017-12-29 01:18:00",null));
		System.out.println(new Date());

		System.out.println(getMonths(2018));
		System.out.println(getMonth(2018,13));
	}
	
}
